package com.qububu.utils;


import java.util.regex.Pattern;

import org.apache.commons.lang3.StringEscapeUtils;

import com.qububu.systemBase.exceptions.ValidateFailException;

/**
 * @author ZCW
 * @功能 参数转换及长度等的验证, 传入参数处理
 * */
public class ParamConverter {
	
	/**
	 * @功能 将传入字符串转换为整数(String to int), 转换失败抛出异常
	 * */
	public static int convertToInteger(String intStr) {
		try {
			return Integer.parseInt(intStr);
		}
		catch(Exception ex) {
			throw new ValidateFailException("Fail to convert input to int type, input=" + intStr, ex);
		}
	}
	
	/**
	 * @功能 将传入字符串转换为Long, 转换失败抛出异常
	 * */
	public static Long convertToLongObj(String longStr) {
		try {
			return Long.decode(longStr);
		}
		catch(Exception ex) {
			throw new ValidateFailException("Fail to convert input to Long type, input=" + longStr, ex);
		}
	}
	
	/**
	 * @功能 将传入字符串转换为Double, 转换失败抛出异常
	 * */
	public static double convertToDouble(String doubleStr) {
		try {
			return Double.parseDouble(doubleStr);
		}
		catch(Exception ex) {
			throw new ValidateFailException("Fail to convert input to double type, input=" + doubleStr, ex);
		}
	}
	
	/**
	 * @功能 对html相关字符进行转义, 返回转义后的字符串
	 * */
	public static String convertToHtmlCapableString(String text) {
		return StringEscapeUtils.escapeHtml4(text);
	}
	
	/**
	 * @功能 验证输入字符串是否超长, 超出长度则抛出异常, 没有超出长度则返回原数据
	 * @param str 需要验证长度的字符串
	 * @param length 需要验证长度的字符串的允许长度
	 * */
	public static String assertMaxLength(String str, int length) {
		if (str.length() > length) {
			throw new ValidateFailException("Input string " + (str.length() > 15 ? str.substring(0, 15) + "..." : str) + "already exceeded the length limit " + length); 
		}
		else {
			return str;
		}
	}
	
	/**
	 * @功能 验证输入字符串是否为空或者长度为0
	 * @return boolean true: 传入数据为空, false: 传入数据不为空
	 * */
	public static boolean isNullOrEmpty(String str) {
		if (str == null || str.isEmpty() ) {
			return true;
		}
		else {
			return false;
		}
	}

	/**
	 * @功能 验证输入字符串是否为空或者长度为0, 如果为空或为0则抛出异常, 不为空或长度不为0则返回原数据
	 * */
	public static String assertNotEmpty(String str) {
		if (isNullOrEmpty(str)) {
			throw new ValidateFailException("input string is null or empty");
		}
		else {
			return str;
		}
	}
	
	/**
	 * @功能 验证输入字符串是否为空, 如果为空则抛出异常, 不为空则返回原数据
	 * */
	public static String assertNotNull(String str) {
		if (str == null) {
			throw new ValidateFailException("input string is null");
		}
		else {
			return str;
		}
	}
	
	/**
	 * @功能 验证输入字符串是否为邮箱数据, 如果不是则抛出异常, 是邮箱则返回原数据
	 * */
	public static String assertEmailString(String str) {
		if (!SystemValidate.validateInput(str, "E")) {
			throw new ValidateFailException("input string is not legal e-mail string:" + str);
		}
		else {
			return str;
		}
	}
	
	/**
	 * @功能 验证输入数据是否为电话号码
	 * */
	/*public static final String assertPhoneNumberString(String str) {
		str = str.trim();
		int minPhoneNumberLength = 8, maxPhoneNumberLength = 11;
		if (!SystemValidate.validateInput(str, minPhoneNumberLength, maxPhoneNumberLength, "0")) {
			throw new ValidateFailException("input string is not legal number string:" + str);
		} else {
			if (str.length() == 11) {
				String phoneNumberBegins[] = {
						"139", "138", "137", "136", "135", "134", "147", "150", "151", "152", "157", "158", "159", "182", "183", "184", "187", "188",// 移动号段
						"130", "131", "132", "155", "156", "185", "186", "145", // 联通号段
						"133", "153", "180", "181", "189"// 电信号段
						};
				for (String begin : phoneNumberBegins) {
					if (str.startsWith(begin)) {
						return str;
					}
				}
			} else if (str.length() == 8) {
				return str;
			}
		}
		
		throw new ValidateFailException("input string is not legal number string:" + str);
//		return assertNumberString(str);
	}*/
	
	public static final String assertPhoneNumberString(String str) {
		str = str.trim();
		String regex = "[0-9\\ \\-]{5,}";
		
		if(Pattern.compile(regex).matcher(str).matches()) {
			return str;
		}
		
		throw new ValidateFailException("input string is not legal phoneNumber string:" + str);
	}
	
	/**
	 * 输入字符串是否为数字
	 * */
	public static final String assertNumberString(String str) {
		if (!SystemValidate.validateInput(str, "0")) {
			throw new ValidateFailException("input string is not legal number string:" + str);
		} else return str;
	}
	
	/**
	 * 验证输入的是时间
	 * */
	public static final String assertIsDateString(String str) {
		str = str.trim();
		if ("today".equals(str) || "tomorrow".equals(str))
			return str;
		String regex = "([0-9]{4}(年|-)(0?[1-9]|1[0-2])(月|-)((0?[1-9])|((1|2)[0-9])|30|31)(日|) ((0|1)?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]))|([0-9]{4}(年|-)(0?[1-9]|1[0-2])(月|-)((0?[1-9])|((1|2)[0-9])|30|31)(日|))";
		
		if (Pattern.compile(regex).matcher(str).matches())
			return str;
		else 
			throw new ValidateFailException("input string is not date string:" + str);
	}
	
	public static void main(String[] args){
		String str = "2013-9-9 23:59:59";
		String str1 =  assertIsDateString(str);
		System.out.println(str);
		System.out.println(str1);
		/*String str = "11";
		String str1 = assertNumberString(str);
		System.out.println(str);
		System.out.println(str1);*/
	}
}
